﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brain.Skinning;
using Microsoft.Xna.Framework.Input;
using Brain.Ballistics3D;

namespace Brain.FPS
{
    public class DefaultGamerWeaponManager : IGamerWeaponManager
    {
        public IGamerController Controller { get; set; }

        public List<Weapon> Weapons { get; set; }

        public int CurrentWeaponIndex { get; set; }
        public Weapon CurrentWeapon { get; set; }

        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        private bool stickWeaponToCharacterHand = false;
        public bool StickWeaponToCharacterHand
        {
            get { return StickWeaponToCharacterHand; }
            set
            {
                stickWeaponToCharacterHand = value;
            }
        }

        public DefaultGamerWeaponManager(IGamerController con)
        {
            this.ShouldUpdate = true;
            this.ShouldDraw = true;

            this.Controller = con;
            Weapons = new List<Weapon>();
        }


        public void Update()
        {
        }

        public void Shoot()
        {
            if (CurrentWeapon != null)
            {
                if (CurrentWeapon.Shoot())
                {
                    if (BulletManager3D.Instance != null)
                    {
                        BulletManager3D.Instance.AddBullet(CurrentWeapon.WorldObject.Position
                            + Vector3.Transform(CurrentWeapon.Info.ShotPosition, CurrentWeapon.WorldObject.RotationMatrix), CurrentWeapon.WorldObject.RotationMatrix.Forward,
                            CurrentWeapon.Info.BulletSpeed, OnHit);
                    }
                }
            }
        }


        public virtual void OnHit(Bullet3D bullet)
        {
        }

        private Vector3 CurrentPosition;
        public void Draw()
        {
            if (stickWeaponToCharacterHand)
            {
                for (int i = 0; i < Weapons.Count; i++)
                {
                    Weapon weapon = Weapons[i];
                    if (i == CurrentWeaponIndex)
                    {
                        CurrentWeapon = weapon;

                        CharacterInfo character = this.Controller.CharInfo;

                        if (Controller.IsScoping)
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, weapon.Info.ScopePosition, 5);
                        }
                        else
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, character.WeaponPosition, 5);
                        }
                    }
                    weapon.Update();
                    weapon.Draw();
                }
            }
            else
            {
                for (int i = 0; i < Weapons.Count; i++)
                {
                    Weapon weapon = Weapons[i];
                    if (i == CurrentWeaponIndex)
                    {
                        CurrentWeapon = weapon;

                        CharacterInfo character = this.Controller.CharInfo;

                        if (Controller.IsScoping)
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, weapon.Info.ScopePosition, 5);
                        }
                        else
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, character.WeaponPosition, 5);
                        }

                        weapon.WorldObject.Position = this.Controller.Camera.Position + Vector3.Transform(CurrentPosition, this.Controller.Camera.RotationMatrix);
                        weapon.WorldObject.RotationRadians = this.Controller.Camera.RotationRadians;
                    }
                    weapon.Update();
                    weapon.Draw();
                }
            }
        }

    }
}
